import time
from func import user
from Database import DatabaseUtil as db
db = db('root','7k7k','wn15')
class account():
    def __call__(self, usern, stm):
        times = str(time.strftime('%F %T'))
        db.update("insert into statements values('{}','{}','{}')".format(usern,stm,times))
        from menu import main_menu
        main_menu()

    def select(self):
        lines = db.select('select * from us')
        from func import user_index
        print('你目前账户金额为'+str(lines[user_index][2]))
        from menu import main_menu
        main_menu()

    def withdraw(self):
        money = int(input('请输入取款金额'))
        from func import user_index, usern
        if money < 0:
            print('请输入正确的金额')
            self.withdraw()
        elif self.check_money(user_index,money):
            print('取出金额'+str(money))
            self.change_money(user_index,-money)
            stm = '取出金额{}'.format(money)
            self(usern,stm)
            from menu import main_menu
            main_menu()
        else:
            print('余额不足')
            self.withdraw()

    def deposite(self):
        money = int(input('请输入存款金额'))
        if money < 0:
            print('请输入正确的金额')
            self.deposite()
        else:
            print('存入金额'+str(money))
            from func import user_index,usern
            self.change_money(user_index,money)
            stm = '存入金额{}'.format(money)
            self(usern,stm)
            from menu import main_menu
            main_menu()

    def transfer(self):
        uname = input('请输入对方账户')
        uid = user().usr_check(uname)
        from func import user_index, usern
        if uid:
            money = int(input('请输入转账金额'))
            if money < 0:
                print('请输入正确的数字')
                self.transfer()
            elif self.check_money(user_index,money):
                print('转入金额'+str(money))
                self.change_money(user_index,-money)
                self.change_money(uid,money)
                stm = '向账户{}转入金额{}'.format(uname,money)
                self(usern,stm)
                from menu import main_menu
                main_menu()
            else:
                print('余额不足')
                self.transfer()
        else:
            print('无效账户')
            from menu import main_menu
            main_menu()

    def statement(self):
        from func import usern
        lines = db.select("select * from statements where uname = '{}'".format(usern))
        print(lines)
        from menu import main_menu
        main_menu()

    def check_money(self,index,a):
        lines = db.select('select * from us')
        money = int(lines[index][2])
        if money > a :
            return True
        else:
            return False
    def change_money(self,index,money):
        lst = db.select('select * from us')
        lst_user = lst[index]
        usn = lst_user[0]
        ubl = lst_user[2]
        ubl += money

        db.update("update us set money={} where uname='{}'".format(ubl,usn))
